<!DOCTYPE html>
<html>
<head>
	<title>新闻咨询登录页面</title>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<link rel="stylesheet" type="text/css" href="css/news.css">
</head>
<body>
	<div id="top">
		<label>Google News</label>    
	</div>
	<div id="center">
		<img id="regtouxiang" src="img/touxiang.jpg">

		<div id="reg_text">
			<input id="usernamae" type="text" name="" placeholder="请输入用户名"><br>
			<input style="width: 80%;height: 50px;margin-top: 20px;
	        margin-left: 10%;border:0px;border-radius: 5px;" id="password" type="password" name="" placeholder="请输入密码"><br>
		<a id="goreg" href="###" style="position: absolute;left: 46%;top:115%;color:green;font-size: 16px;text-decoration: none;letter-spacing: 2px;">还没有账号?去注册</a>
		</div>
		
	</div>
	<div id="foot">
		<input  id="regbtn" type="button" name="" value="登录">
	</div>

	<script src="js/jquery-3.2.1.js"></script>
	<script type="text/javascript">
		$(function(){
            $("#goreg").on("click",function(){
            	window.location.href="news_reg.html";
            });
			$("#usernamae").on("change",function(){
				$.ajax({
					type:"get",
					url:"http://127.0.0.1/news/touxiang.php",
					async:true,
					data: {
						name:$("#usernamae").val()
					},
					dataType:"json",
					success:function(xhr){
						$("img").attr('src',xhr[0].touxiang_img);
					}
				});
			});
			
			
			$("#regbtn").on("click",function(){
				
				$.ajax({
					type: "get",
					url: "http://127.0.0.1/news/login.php",
					async: true,
					data: {
						name:$("#usernamae").val(),
						password:$("#password").val(),
						userimg:$("#regtouxiang").attr('src')
					},
					dataType:"json",
					success: function(data) {
						console.log(data);
						if(data['code']==1){
							window.location.href="index.html";
						}
						else if(data['code']==-10006){
							alert("密码不正确");
						}
						else if(data['code']==-10007){
							alert("用户名不存在");
						}
						else{
							alert("登录失败,请检查网络连接");
						}
					}
				});

			})
		});
	</script>
</body>
</html>